define(["utils", "./leftTreeGroup", "./leftTreePersonItem"], function (utils, Group, PersonItemClass) {

    var Component = function (config) {
        var _this = this;
        this.selector = config.selector;
        this.rootPersonItems = [];
          this.rootGroups = [];
        this.$el = $("<div class='person-selector-treearea'></div>");

    }

    Component.prototype = {
      getResult:function(){
        this.deptResult = [];
        for(var i=0,j= this.rootGroups.length;i<j;i++){
          this.rootGroups[i].getResult()
        }
        return {"dept":this.deptResult};
      },
      clearSelected:function(){
          for (var i = 0, j = this.rootGroups.length; i < j; i++) {
            this.rootGroups[i].clearallCheckChildren();    
          }
      },
        init: function (data, rootPersonData) {
            this.$el.empty();
            var rootDeptData = data["0"];
            if(rootDeptData){
              for (var i = 0, j = rootDeptData.length; i < j; i++) {
                  var group = new Group({
                      selector: this.selector,
                      tree: this,
                      isRoot: true,
                      itemData: rootDeptData[i],
                      index: 0
                  });

                  this.rootGroups.push(group);
                  this.$el.append(group.$el);
              }
          }



        },
        rootPersonItemToggle: function (itemInstance) {
            if (itemInstance.status === "checked") {
                itemInstance.unChecked();
                this.selector.rightTree.removeRootPersonItem(itemInstance.itemData);

                // 从缓存数据中删除人员
                for (var i = 0; i < this.selector.hasSelected.length; i++) {
                    if (this.selector.hasSelected[i].value.accountId == itemInstance.itemData.value.accountId) {
                        this.selector.hasSelected.splice(i, 1);
                    }
                }

            } else {
                itemInstance.checked();
                this.selector.rightTree.addRootPersonItem(itemInstance.itemData);

                // 从缓存数据中增加人员
                this.selector.hasSelected.push(itemInstance.itemData);
            }
        },
    };


    return Component;
});
